    package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*;
//model layer package has to be imported

public class DVDCtr
{   private static DVDCtr dvdCtr;
    private DVDCol dvdCol;
    //declare instance variables to reference containers

    /**
     * Constructor for objects of class DVDCtr
     */
    public DVDCtr()
    {   
        dvdCol = new DVDCol();
        //set up references to containers    
    }

    public static DVDCtr getInstance()
    {
        //Creates a new instance if there is no running instance.
        if(dvdCtr==null)
        {
            dvdCtr= new DVDCtr();
        }
        return dvdCtr;
    }

    //Dummy implementation: after implementing the model layer 
    //objects of class DVD are to be returned, not String
    public String findDVD(long _id)
    {
        //this method is to search the container for a DVD
        //with an id-number equal to the parameter no
        //when the dvd is found, the method is to call print-methods in the TUI layer
        DVD d=dvdCol.findDVD(_id);
        String str = PrintDVD(d); 
        return str; 
    }

    /** Searches for a dvd copy.
     * 
     */
    public Copy findDVDCopy(String _serialNumber)
    {
        Copy cpy = null;
        for (DVD dvd : dvdCol.getDVDCol())
        {
            cpy = dvd.findCopy(_serialNumber);
        }
        return cpy;
    }

    public long findDVDviaSerial(String _serialNumber)
    {
        return dvdCol.findCopyOfDVD(_serialNumber);
    }

    //Checks if a copy of a dvd exists.
    public boolean isDVDCopy(String _serialNumber)
    {
        boolean exist = false;
        if(findDVDCopy(_serialNumber) != null)
        {
            exist = true;
        }
        return exist;
    }

    private String PrintDVD(DVD _d)
    {   
        //Prints a list of information about a dvd.
        String str;
        str = " ID: " +_d.getID();
        str +="\n Title: " + _d.getTitle();
        str +="\n Artist: " + _d.getArtist();
        str +="\n Publication Date: " + _d.getPublicationDate();
        str +="\n Number of Copies: " + _d.copy.size();
        if(!_d.PrintAllCopies().isEmpty())
        {
            str +="\n Copies: " + _d.PrintAllCopies();
        }
        str += "\n";
        return str;
    }
    //Dummy implementation: more parameters are probaly to added
    public void createDVD(long _id, String _title, String _artist, String _publicationDate)
    {      DVD d = new DVD(_id, _title, _artist, _publicationDate);
        if(d!=null)
        {dvdCol.d.add(d);}
        //this method constructs a new dvd object 
        //and stores it in the container
    }

    public void deleteDVD(long _id)
    {   
        DVD d=dvdCol.findDVD(_id);
        if(d!=null)
        {
            dvdCol.d.remove(d);
        }
        //search for the dvd and delete the object from the container
    }

    public void createCopy(long _id, String _serialNumber,String _publishDate, String _purchasePrice)
    {   DVD d=dvdCol.findDVD(_id);
        Copy copy = new Copy(_serialNumber,_publishDate,_purchasePrice);
        d.newCopy(copy);
        //create a copy of the specified dvd and add it to the dvd
    }

    public String listAllDvds()
    {   String str="";
        for(DVD dvd : dvdCol.d)
        { 
            str += PrintDVD(dvd);
        }
        //returns a list of all dvds and their associated copies
        return str;
    }
}
